package com.example.shop.user.controller;

import com.example.shop.common.api.ResponseResult;
import com.example.shop.common.api.ServiceReturn;
import com.example.shop.common.bo.Page;
import com.example.shop.common.enums.SessionAttributeKey;
import com.example.shop.pojo.User;
import com.example.shop.user.dto.GoodsCollection;
import com.example.shop.user.service.GoodsCollectionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user/collection")
public class CollectionController {

    @Autowired
    GoodsCollectionService goodsCollectionService;

    @GetMapping(value = "/browse/{pageIndex}/{pageSize}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<List<GoodsCollection>> browseCollection(HttpSession session, @PathVariable("pageIndex")int pageIndex,@PathVariable("pageSize")int pageSize){
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());

        List<GoodsCollection> collectionInfoList = goodsCollectionService.browseCollection(user,new Page(pageIndex,pageSize));
        return ResponseResult.success(collectionInfoList);
    }

    @GetMapping(value = "/cancel/{goodsId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> cancelCollection(HttpSession session, @PathVariable("goodsId") long goodsId){
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());
        ServiceReturn<Boolean> result = goodsCollectionService.cancelCollection(user, goodsId);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

    @GetMapping(value = "/add/{goodsId}",consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseResult<Boolean> addCollection(HttpSession session, @PathVariable("goodsId") long goodsId){
        User user = (User) session.getAttribute(SessionAttributeKey.NOW_LOGIN_USER.getKey());
        ServiceReturn<Boolean> result = goodsCollectionService.addCollection(user, goodsId);
        return ResponseResult.auto(result.getStateCode(),result.getReturnData());
    }

}
